const db = require('../../db/index')

//模糊查询
async function queryCard ({ cardCode, cnName, password, series, package, rare, typeOne, typeTwo, pageSize = 10, pageNum = 1 }) {
  try {
    let where = '', isMore = ''
    if (cardCode) {
      where += isMore += `t1.code like '%${cardCode}%'`
      isMore = ' and '
    }
    if (cnName) {
      where += isMore += `cn_name like '%${cnName}%'`
      isMore = ' and '
    }
    if (password) {
      where += isMore += `password like '%${password}%'`
      isMore = ' and '
    }
    if (series) {
      where += isMore += `t5.cycle_name = '${series}'`
      isMore = ' and '
    }
    if (package) {
      where += isMore += `t4.detail_name='${package}'`
      isMore = ' and '
    }
    if (rare) {
      where += isMore += `rare='${rare}'`
      isMore = ' and '
    }
    if (typeOne) {
      where += isMore += `type_one ='${typeOne}'`
      isMore = ' and '
    }
    if (typeTwo) {
      where += isMore += `type_two ='${typeTwo}'`
      isMore = ' and '
    }
    if (isMore) {
      where = 'where ' + where
    }
    // console.log(where);
    let result = await db.query(`
    select card.id as cardId,cn_name,t1.code,t1.url,rare,number,cycle_name,detail_name,r3.id as numId,after_carving
    from card 
    inner join release_card_code as r1 on(card.id=r1.card_id) 
    inner join card_code as t1 on(r1.code_id=t1.id)
    inner join release_code_rare_number as r3 on (r3.code_id=t1.id)
    inner join rare as t3 on(t3.id=r3.rare_id)
    inner join release_code_detail_cycle as r4 on (r4.code_id=t1.id)
    inner join package_detail as t4 on (t4.id=r4.detail_id)
    inner join package_cycle as t5 on (t5.id=r4.cycle_id)
    ${where}
    order by t1.code
    `)
    if (result.length === 0) { return { total: 0, content: {} } }
    let _result = [], tag
    result.map((item) => {
      if (!_result.some((val) => val.code === item.code)) {
        tag = _result.push({
          cardId: item.cardId,
          cnName: item.cn_name,
          url: item.url,
          code: item.code,
          cycleName: item.cycle_name,
          detailName: item.detail_name,
          afterCarving: item.after_carving,
          rareNumberAndId: [
            {
              rare: item.rare,
              number: item.number,
              id: item.numId
            }
          ]
        })
      } else {
        _result[tag - 1].rareNumberAndId.push({
          rare: item.rare,
          number: item.number,
          id: item.numId
        })
      }
    })
    tag = result.length
    _result = _result.splice(pageSize * (pageNum - 1), pageSize)
    return { total: tag, content: _result }
  } catch (error) {
    console.log(error);
    throw error
  }
}
//根据id查详情
async function queryCardById (id) {
  try {
    let result = await db.query(`
    select card.id,password,cn_name,jp_name,en_name,type_one,type_two,attribute,race,rank,attack,defense,cn_effect,jp_effect,en_effect
    ,t1.code,t1.url,rare,usability,detail_name,cycle_name
    from card 
    inner join release_card_code as r1 on(card.id=r1.card_id) 
    inner join card_code as t1 on(r1.code_id=t1.id)
    inner join release_card_usability as r2 on (card.id=r2.card_id)
    inner join usability as t2 on(r2.usability_id=t2.id)
    inner join release_code_rare_number as r3 on (r3.code_id=t1.id)
    inner join rare as t3 on(t3.id=r3.rare_id)
    inner join release_code_detail_cycle as r4 on (r4.code_id=t1.id)
    inner join package_detail as t4 on (t4.id=r4.detail_id)
    inner join package_cycle as t5 on (t5.id=r4.cycle_id)
    where card.id='${id}' 
    `)
    if (result.length <= 0) {
      throw new Error('id异常,请重试或联系管理员')
    }
    result = result.reduce((curr, item, key) => {
      if (key === 1) {
        return {
          ...item,
          code_rare_detail_cycle: [{
            code: item.code,
            rare: item.rare,
            series: item.cycle_name,
            package: item.detail_name
          }]
        }
      }
      curr.code_rare_detail_cycle.push({
        code: item.code,
        rare: item.rare,
        series: item.cycle_name,
        package: item.detail_name
      })
      return curr
    })
    return result
  } catch (error) {
    throw error
  }
}
//修改数量
async function changeNumCard (arr) {
  try {
    arr.map(async item => {
      try {
        await db.query(`update release_code_rare_number set number='${item.num}' where id='${item.id}' `)
      } catch (error) {
        return error
      }
    });
    return `已成功更新${arr.length}条数据`
  } catch (error) {
    return error
  }
}
//清空数据库
async function clearDataBase () {
  try {
    await db.query(`truncate table card ;`)
    await db.query(`truncate table card_code;`)
    await db.query(`truncate table package_cycle;`)
    await db.query(`truncate table package_detail;`)
    await db.query(`truncate table release_card_code;`)
    await db.query(`truncate table release_package_cycle;`)
    await db.query(`truncate table release_card_usability;`)
    await db.query(`truncate table release_code_rare_number;`)
    await db.query(`truncate table release_package_cycle;`)
    await db.query(`truncate table release_code_detail_cycle;`)
  } catch (error) {
    throw error
  }
}
module.exports = {
  queryCard, queryCardById, changeNumCard, clearDataBase
}